//
//  MSGameManager.m
//  MagicSword
//
//  Created by Netease on 15/11/20.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSGameManager.h"
#import "MSSelectSprite.h"
#import "MSEnemySprite.h"
#import "MSHeroManager.h"
#import "MSHeroSprite.h"
#import "AStarFinder.h"
#import "MSDBManager.h"
#import "MSItemManager.h"

@implementation MSGameManager {
    NSMutableArray *_heroArray;
    NSMutableArray *_enemyArray;
    AStarFinder    *_aStarFinder;
}

+ (id)shareManager {
    static MSGameManager *gameMgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        gameMgr = [[MSGameManager alloc] init];
    });
    return gameMgr;
}

- (id)init {
    self = [super init];
    if (self) {
        _heroArray = [NSMutableArray array];
        _enemyArray = [NSMutableArray array];
        
    }
    return self;
}

- (MSTileMap *)setMapWithSession:(NSInteger)session chapter:(NSInteger)chapter {
    _session = session;
    _chapter = chapter;
    _roundCount = 1;
    
    _curtitleMap = [[MSTileMap alloc] initWithIndex:_session dot:_chapter];
    _curtitleMap.position = CGPointMake(0, 0);
    _curtitleMap.zPosition = 1;
    _gameScale = ScreenWidth/(_curtitleMap.mapSize.width*_curtitleMap.tileSize.width);
    [_curtitleMap setScale:_gameScale];
    _mapSize = CGSizeMake(_curtitleMap.mapSize.width*_curtitleMap.tileSize.width*_gameScale, _curtitleMap.mapSize.height*_curtitleMap.tileSize.height*_gameScale);
    _tileSize = CGSizeMake(_curtitleMap.tileSize.width*_gameScale, _curtitleMap.tileSize.height*_gameScale);

    _aStarFinder = [[AStarFinder alloc] initWithTileCount:_curtitleMap.mapSize.width*_curtitleMap.mapSize.height];
    
    [self initHero];
    [self initEnemy];
    
    return _curtitleMap;
}

- (BOOL)isLevelUp:(MSHeroSprite *)sprite {
    NSInteger need = [self getLevelUPNeedEx:sprite.heroInfo.level+1];
    if (need > 0 && sprite.heroInfo.Experience > need) {
        sprite.heroInfo.level += 1;
        return YES;
    }
    return NO;
}

- (NSInteger)getLevelUPNeedEx:(NSInteger)level {
    NSArray *upArray = @[@(0),@(15),@(40),@(100),@(180),@(300),@(450),@(620),@(830),@(1000),@(1300)];
    if (upArray.count > level-1) {
        return [upArray[level-1] integerValue];
    }
    
    return 0;
}

- (void)initEnemy {
    [_enemyArray removeAllObjects];
    for (MSEnemyInfo *info in _curtitleMap.enemyArray) {
        MSEnemySprite *enemy = [[MSEnemySprite alloc] initWithEnemyInfo:info withSize:_tileSize];
        enemy.position = [self PointFromCoord:info.point];
        [_enemyArray addObject:enemy];
    }
}

- (void)initHero {
    NSArray *heroArray = [[MSHeroManager shareManager] heroArray];
    [_heroArray removeAllObjects];
    int i = 0;
    for (MSHeroInfo *info in heroArray) {
        if (info.isInFight && _curtitleMap.heroPointArray.count > i) {
            NSValue *pointValue = _curtitleMap.heroPointArray[i];
            info.HP = info.MaxHP;
            MSHeroSprite *hero = [[MSHeroSprite alloc] initWithHeroInfo:info withSize:_tileSize];
            hero.position = [self PointFromCoord:[pointValue CGPointValue]];
            [_heroArray addObject:hero];
            i++;
        }
    }
}

- (void)addHeroWithSession:(NSInteger)session chapter:(NSInteger)chapter {
    [self addHeroWithSession:session chapter:chapter inGameBegin:YES];
}

- (void)addHeroWithSession:(NSInteger)session chapter:(NSInteger)chapter inGameBegin:(BOOL)inGameBegin {
    if (session==0 && chapter==1 && inGameBegin) {
        [self addNewHeroWithType:MSHeroModel_B];
        [self addNewHeroWithType:MSHeroModel_C];
    } else if (session==0 && chapter==2 && !inGameBegin) {
        [self addNewHeroWithType:MSHeroModel_D];
    }
}

- (void)addNewHeroWithType:(MSHeroModel)type {
    NSMutableArray *heroArray = [[MSHeroManager shareManager] heroArray];
    
    if (![self isHaveHeroWithModel:type heroArray:heroArray]) {
        MSHeroInfo *hero = [MSHeroManager createHeroWithModel:type];
        [heroArray addObject:hero];
        if (_heroArray.count < [[MSDBManager shareManager] getMaxHeroCount]) {
            hero.isInFight = YES;
        }
        [ProgressHUD showSuccess:@"英雄加入"];
        
        if (_curtitleMap.heroPointArray.count > _heroArray.count) {
            NSValue *pointValue = _curtitleMap.heroPointArray[_heroArray.count];
            MSHeroSprite *heroSprite = [[MSHeroSprite alloc] initWithHeroInfo:hero withSize:_tileSize];
            heroSprite.position = [self PointFromCoord:[pointValue CGPointValue]];
            [_heroArray addObject:heroSprite];
        }
    }
}

- (void)getItemWithHero:(MSHeroInfo *)heroInfo point:(CGPoint)point {
    // item
    CGPoint coord = [self CoordFromPoint:point];
    int item_tileGid = [_curtitleMap.item tileGidAt:coord];
    
    if (item_tileGid) {
        NSDictionary *props = [_curtitleMap propertiesForGid:item_tileGid];
        NSString *value = [props valueForKey:@"type"];
        int type = [value intValue];
        if (type > 0) {
            NSString *msg = [MSItemManager getItemWithType:type hero:heroInfo];
            if (msg) {
                [ProgressHUD showSuccess:msg];
            }
            [_curtitleMap.item removeTileAtCoord:[_curtitleMap.item coordForPoint:coord]];
        }
    }
}

- (BOOL)isHaveHeroWithModel:(MSHeroModel)type heroArray:(NSArray *)heroArray {
    for (MSHeroInfo *info in heroArray) {
        if (type == info.type) {
            return YES;
        }
    }
    return NO;
}

- (NSArray *)getEnemyArray {
    return _enemyArray;
}

- (void)removeEnemy:(MSEnemySprite *)enemy {
    [_enemyArray removeObject:enemy];
    [enemy removeFromParent];
    
    if (_enemyArray.count == 0) {
        // 成功，战斗胜利
        if (_delegate && [_delegate respondsToSelector:@selector(gameOver:)]) {
            [_delegate gameOver:YES];
        }
    }
}

- (void)removeHero:(MSHeroSprite *)hero {
    [_heroArray removeObject:hero];
    [hero removeFromParent];
    
    if (_heroArray.count == 0) {
        // 失败，战斗结束
        if (_delegate && [_delegate respondsToSelector:@selector(gameOver:)]) {
            [_delegate gameOver:NO];
        }
    }
}

- (NSArray *)getHeroArray {
    return _heroArray;
}

- (MSHeroSprite *)getFirstNormalHero {
    for (MSHeroSprite *sprite in _heroArray) {
        if (sprite.heroStatus == MSHeroSpriteStatus_Normal) {
            return sprite;
        }
    }
    
    return nil;
}

// 获得某个等级所需要的经验值
- (NSInteger)getLevelNeedExperience:(NSInteger)level {
    NSArray *expArray = @[@(0),@(15),@(25),@(30),@(40),@(50),@(65),@(80),@(93),@(110),@(130),@(150),@(173),@(200),@(230),@(266),@(300)];
    
    return [expArray[level] integerValue];
}

- (NSArray *)getHeroMoveArray:(CGPoint)curPoint canMoveValue:(NSInteger)moveValue {
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *pointValue = [NSMutableArray array];
    int i,j;
    for (i=curPoint.x-moveValue; i<=curPoint.x+moveValue; i++) {
        for (j=curPoint.y-moveValue; j<=curPoint.y+moveValue; j++) {

            if (i<0||j<0||i>=_curtitleMap.mapSize.width||j>=_curtitleMap.mapSize.height) {
                continue;
            }
            if ((ABS(i-curPoint.x)+ABS(j-curPoint.y)) > moveValue) {
                continue;
            }
            if (i==curPoint.x && j==curPoint.y) {
                continue;
            }
            
            
            MSSelectSprite *select = [MSSelectSprite spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(_curtitleMap.tileSize.width-1, _curtitleMap.tileSize.height-1)];
            select.position = [self PointFromCoord:CGPointMake(i, j)];
            select.coord = CGPointMake(i, j);
        
            [select setStatus:MSSeleteSpriteStatus_Cannot];
            if ([self isCanMoveWithCoord:CGPointMake(i, j)]) {
                [select setStatus:MSSeleteSpriteStatus_Normal];
            }
            
            [array addObject:select];
            if (select.status == MSSeleteSpriteStatus_Normal) {
                [pointValue addObject:[NSValue valueWithCGPoint:CGPointMake(i, j)]];
            }
        }
    }
    [_aStarFinder setEnableTile:pointValue];
    
    [self clearCannotMoveToSelect:array from:curPoint];
    return array;
}

- (MSHeroSprite *)getEnemyAttackWithCoord:(CGPoint)curPoint canAttackValue:(MSAttackDistanceType)attackDistance {
    NSMutableArray *array = [NSMutableArray array];
    int i,j;
    int distance = 1;
    if (attackDistance>MSAttackDistanceType_A) {
        distance = 2;
    }
    for (i=curPoint.x-distance; i<=curPoint.x+distance; i++) {
        for (j=curPoint.y-distance; j<=curPoint.y+distance; j++) {
            
            if (i<0||j<0||i>=_curtitleMap.mapSize.width||j>=_curtitleMap.mapSize.height) {
                continue;
            }
            if ((ABS(i-curPoint.x)+ABS(j-curPoint.y)) > distance) {
                continue;
            }
            if (i==curPoint.x && j==curPoint.y) {
                continue;
            }
            
            if (MSAttackDistanceType_B == attackDistance && (ABS(i-curPoint.x)+ABS(j-curPoint.y)) == 1) {
                continue;
            }
            
            MSHeroSprite *sprite = [self getHeroWithCoord:CGPointMake(i, j)];
            if (sprite) {
                [array addObject:sprite];
            }
        }
    }
    
    // 取血最少的
    MSHeroSprite *minSprite;
    for (MSHeroSprite *sprite in array) {
        if (!minSprite) {
            minSprite = sprite;
        } else {
            if (sprite.heroInfo.HP < minSprite.heroInfo.HP) {
                minSprite = sprite;
            }
        }
    }
    return minSprite;
}

- (CGPoint)moveEnemyNearByHero:(CGPoint)curCoord canMoveValue:(NSInteger)moveValue {
    // 找到离自己最近的英雄位置
    CGPoint heroCoord = CGPointMake(-1, -1);
    for (MSHeroSprite *sprite in _heroArray) {
        CGPoint coord = [self CoordFromPoint:sprite.position];
        if (heroCoord.x==-1 && heroCoord.y==-1) {
            heroCoord = coord;
        } else {
            float d = sqrtf((curCoord.x-coord.x)*(curCoord.x-coord.x)+(curCoord.y-coord.y)*(curCoord.y-coord.y));
            float mind = sqrtf((curCoord.x-heroCoord.x)*(curCoord.x-heroCoord.x)+(curCoord.y-heroCoord.y)*(curCoord.y-heroCoord.y));
            if (d < mind) {
                heroCoord = coord;
            }
        }
    }
    
    // 找到可以移动的位置离英雄最近
    CGPoint minCoord = CGPointMake(-1, -1);
    NSArray *array = [self getHeroMoveArray:curCoord canMoveValue:moveValue];
    for (MSSelectSprite *select in array) {
        if (select.status ==  MSSeleteSpriteStatus_Normal) {
            if (minCoord.x==-1 && minCoord.y==-1) {
                minCoord = select.coord;
            } else {
                float d = sqrtf((heroCoord.x-select.coord.x)*(heroCoord.x-select.coord.x)+(heroCoord.y-select.coord.y)*(heroCoord.y-select.coord.y));
                float mind = sqrtf((heroCoord.x-minCoord.x)*(heroCoord.x-minCoord.x)+(heroCoord.y-minCoord.y)*(heroCoord.y-minCoord.y));
                if (d < mind) {
                    minCoord = select.coord;
                }
            }
        }
    }
    return minCoord;
}

- (void)clearCannotMoveToSelect:(NSArray *)array from:(CGPoint)from {
    for (MSSelectSprite *select in array) {
        if (select.status == MSSeleteSpriteStatus_Normal) {
            if (![self MoveSpriteTo:select.coord fromPoint:from]) {
                [select setStatus:MSSeleteSpriteStatus_Cannot];
            }
        }
    }
}

- (NSArray *)getHeroAttackArray:(CGPoint)curPoint canAttackDistance:(MSAttackDistanceType)attackDistance {
    NSMutableArray *array = [NSMutableArray array];
    int i,j;
    int distance = 1;
    if (attackDistance>MSAttackDistanceType_A) {
        distance = 2;
    }
    for (i=curPoint.x-distance; i<=curPoint.x+distance; i++) {
        for (j=curPoint.y-distance; j<=curPoint.y+distance; j++) {
            
            if (i<0||j<0||i>=_curtitleMap.mapSize.width||j>=_curtitleMap.mapSize.height) {
                continue;
            }
            if ((ABS(i-curPoint.x)+ABS(j-curPoint.y)) > distance) {
                continue;
            }
            if (i==curPoint.x && j==curPoint.y) {
                continue;
            }
            
            if (MSAttackDistanceType_B == attackDistance && (ABS(i-curPoint.x)+ABS(j-curPoint.y)) == 1) {
                continue;
            }
            
            MSSelectSprite *select = [MSSelectSprite spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(_curtitleMap.tileSize.width-1, _curtitleMap.tileSize.height-1)];
            select.position = [self PointFromCoord:CGPointMake(i, j)];
            select.coord = CGPointMake(i, j);
            if ([self isEnemy:CGPointMake(i, j)]) {
                [select setStatus:MSSeleteSpriteStatus_Attack];
            } else {
                [select setStatus:MSSeleteSpriteStatus_Cannot];
            }
            
            [array addObject:select];
        }
    }
    
    return array;
}

- (void)moveto:(CGPoint)coord sprite:(SKSpriteNode *)sprite {
    [self moveto:coord sprite:sprite completion:nil];
}

- (void)openDoorWithType:(int)type {
    [_curtitleMap openDoorWithType:type];
}

- (void)moveto:(CGPoint)coord sprite:(SKSpriteNode *)sprite completion:(MSCompletionBlock)block {
    CGPoint curCoord = [self CoordFromPoint:sprite.position];
    SKAction *action = [self getMoveAction:coord curCoord:curCoord];
    if (coord.x==curCoord.x && coord.y==curCoord.y) {
        action = [SKAction waitForDuration:0.1];
    }
    [sprite runAction:action completion:^{
        if (block) {
            block();
        }
    }];
}

- (SKAction *)getMoveAction:(CGPoint)coord curCoord:(CGPoint)curCoord {

    NSMutableArray *actionArray = [NSMutableArray array];
    [self MoveSpriteTo:coord fromPoint:curCoord];
    for (TilePoint *tpoint in _aStarFinder.runTable) {
        CGPoint point = [self PointFromCoord:CGPointMake(tpoint.x, tpoint.y)];
        SKAction *action = [SKAction moveTo:point duration:0.1];
        [actionArray addObject:action];
    }
    
    return [SKAction sequence:actionArray];
}

- (BOOL)MoveSpriteTo:(CGPoint)coord fromPoint:(CGPoint)curCoord {
    TilePoint *start = [[TilePoint alloc] initWitTilePoint:curCoord.x Y:curCoord.y];
    TilePoint *end = [[TilePoint alloc] initWitTilePoint:coord.x Y:coord.y];
    return [_aStarFinder start:start EndPoint:end];
}

- (void)moveMapTo:(float)top {
    [self moveMapTo:top animated:NO];
}

- (void)moveMapWithSpriteTop:(float)top animated:(BOOL)animated {
    [self moveMapTo:(ScreenHeight/2)-top-ScreenHeight/6 animated:YES];
}

- (void)moveMaptToNextNormalHero {
    MSHeroSprite *sprite = [self getFirstNormalHero];
    if (sprite) {
        [self moveMapWithSpriteTop:sprite.position.y animated:YES];
    }
}

- (void)moveMapTo:(float)top animated:(BOOL)animated {
    
    if (top > 0) {
        top = 0;
    } else if (top<ScreenHeight-_mapSize.height) {
        top = ScreenHeight-_mapSize.height;
    }
    
    if (animated) {
        SKAction *action = [SKAction moveToY:top duration:0.2];
        [_curtitleMap runAction:action];
    } else {
        _curtitleMap.position = CGPointMake(0, top);
    }
}

- (CGPoint)PointFromCoord:(CGPoint)point {
    return CGPointMake(point.x*_curtitleMap.tileSize.width+_curtitleMap.tileSize.width/2, point.y*_curtitleMap.tileSize.height+_curtitleMap.tileSize.height/2);
}

- (CGPoint)CoordFromPoint:(CGPoint)point {
    return CGPointMake((int)((point.x-_curtitleMap.tileSize.width/2)/_curtitleMap.tileSize.width), (int)((point.y-_curtitleMap.tileSize.height/2)/(_curtitleMap.tileSize.height)));
}

- (BOOL)isCanMoveWithCoord:(CGPoint)point {
    if([_curtitleMap.wall tileGidAt:point]) {
        return NO;
    }
    
    if([_curtitleMap.npc tileGidAt:point]) {
        return NO;
    }
    
    if([_curtitleMap.door tileGidAt:point]) {
        return NO;
    }
    
    for (MSEnemySprite *sprite in _enemyArray) {
        CGPoint sp = [self CoordFromPoint:sprite.position];
        if (sp.x == point.x && sp.y == point.y) {
            return NO;
        }
    }
    
    for (MSHeroSprite *sprite in _heroArray) {
        CGPoint sp = [self CoordFromPoint:sprite.position];
        if (sp.x == point.x && sp.y == point.y) {
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)isEnemy:(CGPoint)coord {
    return [self getEnemyWithCoord:coord]==nil ? NO : YES;
}

- (MSEnemySprite *)getEnemyWithPoint:(CGPoint)point {
    return [self getEnemyWithCoord:[self CoordFromPoint:point]];
}

- (MSEnemySprite *)getEnemyWithCoord:(CGPoint)coord {
    for (MSEnemySprite *sprite in _enemyArray) {
        CGPoint sp = [self CoordFromPoint:sprite.position];
        if (sp.x == coord.x && sp.y == coord.y) {
            return sprite;
        }
    }
    
    return nil;
}

- (MSHeroSprite *)getHeroWithCoord:(CGPoint)coord {
    for (MSHeroSprite *sprite in _heroArray) {
        CGPoint sp = [self CoordFromPoint:sprite.position];
        if (sp.x == coord.x && sp.y == coord.y) {
            return sprite;
        }
    }
    
    return nil;
}

- (MSFightScene *)attackEnemy:(MSHeroSprite *)hero enemyPoint:(CGPoint)enemyPoint {
    return [self attackEnemy:hero enemy:[self getEnemyWithPoint:enemyPoint]];
}

- (MSFightScene *)attackEnemy:(MSHeroSprite *)hero enemy:(MSEnemySprite *)enemy {
    return [self attackEnemy:hero enemy:enemy completion:nil];
}

- (MSFightScene *)attackEnemy:(MSHeroSprite *)hero enemy:(MSEnemySprite *)enemy completion:(MSCompletionBlock)block {
    return [self attackEnemy:hero enemy:enemy isHeroAttack:YES completion:block];
}

- (MSFightScene *)attackEnemy:(MSHeroSprite *)hero enemy:(MSEnemySprite *)enemy isHeroAttack:(BOOL)isHeroAttack completion:(MSCompletionBlock)block {
    MSFightScene *scene = [MSFightScene spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(480*ScreenGameScale, 260*ScreenGameScale)];
    scene.position = CGPointMake(0, 0);
    [scene fightWithHeroInfo:hero.heroInfo enemy:enemy.enemyInfo isHeroAttack:isHeroAttack completion:^{
        if (enemy.enemyInfo.HP <= 0) {
            [self removeEnemy:enemy];
        }
        if (hero.heroInfo.HP <= 0 ) {
            [self removeHero:hero];
        }
        [self checkWeaponIsOutUse:hero.heroInfo];
        if (block) {
            block();
        }
    }];
    return scene;
}

- (CGFloat)attackHPEnemy:(MSHeroInfo *)hero enemy:(MSEnemyInfo *)enemy {
    float hp = hero.Attack+hero.curWeapon.Attack;// - enemy.Defense;
    hp = [self getRadomMissHP:hp];
    
    // 武器克性
    if ([self isWeaponWin:hero.curWeapon.type dWeapon:enemy.weaponInfo.type]) {
        hp = hp*1.2;
    }
    
    // 躲避
    if ([self isRandomRing:enemy.MissValue]) {
        hp = 0;
    }
    
    if (enemy.HP > hp) {
        enemy.HP -= hp;
    } else {
        enemy.HP = 0;
    }
    
    // 武器使用次数-1
    hero.curWeapon.useCount --;
    return hp;
}

- (void)checkWeaponIsOutUse:(MSHeroInfo *)hero {
    if (hero.curWeapon.useCount <= 0) {
        [ProgressHUD showError:[NSString stringWithFormat:@"%@ 已破损", hero.curWeapon.name]];
        [hero.weaponList removeObject:hero.curWeapon];
        if (hero.weaponList.count > 0) {
            hero.curWeapon = [hero.weaponList firstObject];
        } else {
            hero.curWeapon = nil;
        }
    }
}

- (BOOL)isCanDoubleHit:(MSHeroInfo *)hero enemy:(MSEnemyInfo *)enemy isHeroAttack:(BOOL)isHeroAttack {
    int rando = 10;
    if (isHeroAttack) {
        rando += hero.DoubleHit + hero.curWeapon.DoubleHit;
        if ([self isWeaponWin:hero.curWeapon.type dWeapon:enemy.weaponInfo.type]) {
            rando += 10;
        };
    } else {
        rando += enemy.DoubleHit;
        if ([self isWeaponWin:enemy.weaponInfo.type dWeapon:hero.curWeapon.type]) {
            rando += 10;
        };
    }
    
    return [self isRandomRing:rando];
}

- (BOOL)isCanBackHit:(MSHeroInfo *)hero enemy:(MSEnemyInfo *)enemy isHeroAttack:(BOOL)isHeroAttack {
    int rando = 40;
    if (isHeroAttack) {
        if ([self isWeaponWin:enemy.weaponInfo.type dWeapon:hero.curWeapon.type]) {
            rando += 20;
        };
    } else {
        if ([self isWeaponWin:hero.curWeapon.type dWeapon:enemy.weaponInfo.type]) {
            rando += 20;
        };
    }
    
    return [self isRandomRing:rando];
}

- (CGFloat)attackHPHero:(MSHeroInfo *)hero enemy:(MSEnemyInfo *)enemy {
    float hp = enemy.Attack + enemy.weaponInfo.Attack;
    hp = [self getRadomMissHP:hp];
    
    // 武器克性
    if ([self isWeaponWin:enemy.weaponInfo.type dWeapon:hero.curWeapon.type]) {
        hp = hp*1.2;
    }
    
    // 躲避
    if ([self isRandomRing:hero.MissValue]) {
        hp = 0;
    }
    
    if (hero.HP > hp) {
        hero.HP -= hp;
    } else {
        hero.HP = 0;
    }
    
    return hp;
}

- (CGFloat)getRadomMissHP:(CGFloat)hp {
    if (hp > 0) {
        int rando = (int)(hp/5);
        if (rando > 0) {
            hp = hp + random()%rando;
        }
    } else {
        hp = random()%3;
    }
    return hp;
}

- (BOOL)isWeaponWin:(MSWeaponType)attackWeapon dWeapon:(MSWeaponType)dWeapon {
    // 剑-》斧
    if (attackWeapon==MSWeaponType_Sword && dWeapon==MSWeaponType_Axe) {
        return YES;
    }
    
    // 斧-》枪
    if (attackWeapon==MSWeaponType_Axe && dWeapon==MSWeaponType_Spear) {
        return YES;
    }
    
    // 枪-》剑
    if (attackWeapon==MSWeaponType_Spear && dWeapon==MSWeaponType_Sword) {
        return YES;
    }
    
    return NO;
}

// 是否完成英雄回合
- (BOOL)checkFinishHeroRound {
    BOOL flag = YES;
    for (MSHeroSprite *sprite in _heroArray) {
        if (sprite.heroStatus != MSHeroSpriteStatus_Finish) {
            flag = NO;
            break;
        }
    }
    
    return flag;
}

- (void)reloadHeroRound {
    for (MSHeroSprite *sprite in _heroArray) {
        [sprite setHeroStatus:MSHeroSpriteStatus_Normal];
    }
    
    _roundCount ++;
}

// 敌方回合进行
- (NSArray *)runEnemyRound {
    NSMutableArray *array = [NSMutableArray array];
    for (MSEnemySprite *sprite in _enemyArray) {
        [array addObject:[MSAIManager AIWithType:sprite.enemyInfo.AIType enmey:sprite]];
    }
    
    return array;
}

// 获得一点几率的成功值 r:0-100
- (BOOL)isRandomRing:(NSInteger)r {
    NSInteger a = random()%100;
//    NSLog(@"###随机数:%d,概率:%d", a, r);
    if (a<=r) {
        return YES;
    } else {
        return NO;
    }
}

// 过关
- (void)passChapter:(NSInteger)session chapter:(NSInteger)chapter {
    NSInteger passSeesion = session;
    
    
    NSInteger passChapter = chapter+1;
    if (chapter == 10) {
        passSeesion ++;
        passChapter = 0;
    }
    [[MSDBManager shareManager] savePassSession:passSeesion Chapter:passChapter];
    
    // 开发英雄数
    if (passSeesion==1 && passChapter==6) {
        [[MSDBManager shareManager] saveMaxHeroCount:4];
        [ProgressHUD showSuccess:@"第4位战斗英雄已经解锁"];
    }
    if (passSeesion==1 && passChapter==10) {
        [[MSDBManager shareManager] saveMaxHeroCount:5];
        [ProgressHUD showSuccess:@"第5位战斗英雄已经解锁"];
    }
    if (passSeesion==2 && passChapter==3) {
        [[MSDBManager shareManager] saveMaxHeroCount:6];
        [ProgressHUD showSuccess:@"第6位战斗英雄已经解锁"];
    }
    
    // 获得英雄
    [self addHeroWithSession:session chapter:chapter inGameBegin:NO];
}
@end
